//
//  IntegralStore.m
//  YYPHSpeedJump
//
//  Created by sujiang on 15/12/22.
//  Copyright © 2015年 sujiang. All rights reserved.
//

#import "IntegralStore.h"
#import "IntergralCell.h"
#import "IntergralRecordListViewController.h"
#import "MallModel.h"

@interface IntegralStore ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
{
    NSInteger page;
}


@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation IntegralStore

- (void)viewDidLoad {
    [super viewDidLoad];
    page = 1;
    self.view.backgroundColor = kColors(239, 239, 244, 1);
    self.title = @"积分商城";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"兑换记录" style:(UIBarButtonItemStylePlain) target:self action:@selector(recordList)];
    [self addSubviews];
    [self requestData];
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)addSubviews
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
    layout.itemSize = CGSizeMake((kScreenWidth - 33) / 2, 186);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64) collectionViewLayout:layout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"IntergralCell" bundle:nil] forCellWithReuseIdentifier:@"IntergralCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
}

- (void)requestData
{
    // /app.do?cmd=intergral_mall
    NSString *url = [NSString stringWithFormat:@"%@/app.do?cmd=integral_mall", kBaseUrl];
    
    [NetworkHandle getUpDataWithUrl:url class:NSClassFromString(@"MallModel") dictionary:@{@"page":[NSNumber numberWithInteger:page]} firstKey:@"data" secondKey:@"list" success:^(NSArray *array) {
        
        if (page == 1) {
            [self.dataArray removeAllObjects];
        }
        [self.dataArray addObjectsFromArray:array];
        if (self.dataArray.count >= 10 && self.collectionView.footer == nil) {
            self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
        }
        
        [self.collectionView.footer endRefreshing];
        [self.collectionView.header endRefreshing];
        [self.collectionView reloadData];
    }];
    
    
}

#pragma mark -- 下拉刷新加载
- (void)headerRefresh
{
    page = 1;
    [self requestData];
}

- (void)footerRefresh
{
    page++;
    [self requestData];
}
#pragma mark -- collectionView代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.dataArray.count > 0) {
        return self.dataArray.count;
    }
    return 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IntergralCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IntergralCell" forIndexPath:indexPath];
    MallModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    collectionView.userInteractionEnabled = NO;
    // /user.do?cmd=integral_exchange&u=65&mall_id=1
    MallModel *model = self.dataArray[indexPath.row];
    NSDictionary *dict = @{@"u":[[NSUserDefaults standardUserDefaults] objectForKey:@"user_id"], @"mall_id":model.mall_id};
    NSString *url = [NSString stringWithFormat:@"%@/user.do?cmd=integral_exchange", kBaseUrl];
    [NetworkHandle getInfoWithUrl:url dictionary:dict success:^(NSDictionary *dictionary) {
        collectionView.userInteractionEnabled = YES;
        if ([dictionary[@"code"] integerValue] == 200) {
            [Tools showAlert:dictionary[@"message"]];
        } else {
            [Tools showAlert:dictionary[@"message"]];
        }
    }];
}




#pragma mark -- 按钮点击事件

- (void)recordList
{
    IntergralRecordListViewController *VC = [[IntergralRecordListViewController alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
    VC.recordType = @"exchange_record";
}

@end
